package com.database.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.database.common.BaseResultCodeEnum;
import com.database.common.ControllerErrorProcessor;
import com.database.common.result.CommonResult;
import com.database.common.result.ListResult;
import com.database.common.result.SingleResult;
import com.database.model.Address;
import com.database.model.Collect;
import com.database.model.Food;
import com.database.model.Menu;
import com.database.model.Order;
import com.database.model.Shop;
import com.database.model.User;
import com.database.model.param.OrderParam;
import com.database.model.param.UserParam;
import com.database.model.vo.CollectVo;
import com.database.model.vo.OrderVo;
import com.database.service.UserService;

@Controller
@RequestMapping("user/")
public class UserController {
	@Autowired
	private UserService userService;
	Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 用户登录验证
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult userLogin(@RequestBody User user) {
		try {
			User value = userService.login(user);
			SingleResult<User> result = new SingleResult<User>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户添加收藏地址
	 * @param address
	 * @return
	 */
	@RequestMapping(value = "addAddress", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addAddress(@RequestBody Address address) {
		try {
			int value = userService.insertAddress(address);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 用户注册
	 * 
	 * @param userParam
	 * @return
	 */
	@RequestMapping(value = "registeUser", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult registeUser(@RequestBody UserParam userParam) {
		try {
			int value = userService.registeUser(userParam);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 用户查看所有店铺
	 * 
	 * @return
	 */
	@RequestMapping(value = "findShopList", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findShopList() {
		try {
			List<Shop> value = userService.findShopList();
			ListResult<Shop> result = new ListResult<Shop>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 用户查看店铺菜单
	 * 
	 * @return
	 */
	@RequestMapping(value = "findFoodList", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findFoodList(@RequestBody Food food) {
		try {
			List<Food> value = userService.findFoodList(food);
			ListResult<Food> result = new ListResult<Food>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户查看订单列表
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "findOrderList", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findOrderList(@RequestBody Order order) {
		try {
			List<OrderVo> value = userService.findOrderList(order);
			ListResult<OrderVo> result = new ListResult<OrderVo>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户查看单个订单
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "findOrder", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findOrder(@RequestBody Menu menu) {
		try {
			List<Menu> value = userService.findOrder(menu);
			ListResult<Menu> result = new ListResult<Menu>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户评价订单
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "estimateOrder", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult estimateOrder(@RequestBody Order order) {
		try {
			int value = userService.estimateOrder(order);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户查看钱包
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "findBalance", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findBalance(@RequestBody User user) {
		try {
			User value = userService.findBalance(user);
			SingleResult<Double> result = new SingleResult<Double>(BaseResultCodeEnum.ERR_CODE_200, value.getBalance());
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户充值
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "addBalance", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addBalance(@RequestBody User user) {
		try {
			Integer value = userService.addBalance(user);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户查看收藏地址
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "selectAddressByUserId", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult selectAddressByUserId(@RequestBody User user) {
		try {
			List<Address> value = userService.selectAddressByUserId(user);
			ListResult<Address> result = new ListResult<Address>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户删除收藏地址
	 * @param address
	 * @return
	 */
	@RequestMapping(value = "deleteAddressByKey", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult deleteAddressByKey(@RequestBody Address address) {
		try {
			Integer value = userService.deleteAddressByKey(address);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户更新收藏地址
	 * @param address
	 * @return
	 */
	@RequestMapping(value = "updateAddressByKey", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateAddressByKey(@RequestBody Address address) {
		try {
			Integer value = userService.updateAddressByKey(address);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户更新个人信息
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "updateUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateUserInfo(@RequestBody User user) {
		try {
			Integer value = userService.updateUserInfo(user);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	/**
	 * 用户提交订单
	 * @param orderParam
	 * @return
	 */
	@RequestMapping(value = "createOrder", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult createOrder(@RequestBody OrderParam orderParam) {
		try {
			Integer value = userService.createOrder(orderParam);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	
	/**
	 * 用户查看收藏店铺
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "findCollectShopList", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findCollectShopList(@RequestBody Collect collect) {
		try {
			List<CollectVo> value = userService.findCollectShopList(collect);
			ListResult<CollectVo> result = new ListResult<CollectVo>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	
	/**
	 * 用户添加收藏店铺
	 * @param orderParam
	 * @return
	 */
	@RequestMapping(value = "aadCollectShop", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult aadCollectShop(@RequestBody Collect collect) {
		try {
			Integer value = userService.aadCollectShop(collect);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	
	/**
	 * 用户查看收藏店铺
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "selectLikeAddress", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult selectLikeAddress(@RequestBody Shop shop) {
		try {
			List<Shop> value = userService.selectLikeAddress(shop);
			ListResult<Shop> result = new ListResult<Shop>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
	
}
